﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PersonalAlbum.Data.Objects;
using PersonalAlbum.Utility;

namespace PersonalAlbum.Data.Entities
{
    public class AlbumRepository : IAlbumRepository
    {
        private string _connectionString;
        public AlbumRepository(string connectionString)
        {
            this._connectionString = connectionString;
        }
        public List<PersonalAlbum.Data.Objects.Album> GetItems()
        {
            return GetItems(true);
        }
        public List<Album> GetItems(bool visible)
        {
            using (var DB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                return DB.Album.Where(a => visible ? a.Visible : true).Select(s => new PersonalAlbum.Data.Objects.Album()
                {
                    Id = s.Id,
                    Name = s.Name,
                    SectionId = s.SectionId,
                    Timestamp = s.Timestamp,
                    Visible = s.Visible,
                    Url = s.Url,
                    Description = s.Description
                }).ToList();
            }
        }
        public void AddItem(Album album)
        {
            using (var DB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                ImagesDal.Album a = new ImagesDal.Album() { Visible = album.Visible, Timestamp = album.Timestamp, SectionId = album.SectionId, Name = album.Name, Url = album.Url, Description = album.Description };
                DB.AddToAlbum(a);
                DB.SaveChanges();
                album.Id = a.Id;
            }
        }

        public void UpdateItem(Album album)
        {
            using (var DB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                ImagesDal.Album old = DB.Album.FirstOrDefault(a => a.Id == album.Id);
                if (old != null)
                {
                    old.Name = album.Name;
                    old.Description = album.Description;
                    old.SectionId = album.SectionId;
                    old.Url = album.Url;
                    old.Visible = album.Visible;
                    DB.SaveChanges();
                }
            }
        }

        public void DeleteItem(Album album)
        {
            using (var DB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                ImagesDal.Album old = DB.Album.FirstOrDefault(a => a.Id == album.Id);
                if (old != null)
                {
                    DB.DeleteObject(old);
                    DB.SaveChanges();
                }
            }
        }


        public Album GetItemById(int albumId)
        {
            using (var DB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                return (from a in DB.Album
                        where a.Id == albumId
                        select new PersonalAlbum.Data.Objects.Album()
                        {
                            Description = a.Description,
                            Id = a.Id,
                            Name = a.Name,
                            SectionId = a.SectionId,
                            Timestamp = a.Timestamp,
                            Url = a.Url,
                            Visible = a.Visible
                        }).SingleOrDefault();

            }
        }

        public List<PersonalAlbum.Data.Objects.Album> GetAlbumsBySectionId(int sectionId, bool visible)
        {
            using (var DB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                return DB.Album.Where(a => a.SectionId == sectionId && (visible ? a.Visible : true)).Select(s => new PersonalAlbum.Data.Objects.Album()
                {
                    Id = s.Id,
                    Name = s.Name,
                    SectionId = s.SectionId,
                    Timestamp = s.Timestamp,
                    Visible = s.Visible,
                    Url = s.Url,
                    Description = s.Description
                }).ToList();
            }
        }


    }
}
